package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.exception.GuliException;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.vo.ChapterVO;
import com.example.eduservice.entity.vo.VideoVO;
import com.example.eduservice.mapper.EduChapterMapper;
import com.example.eduservice.mapper.EduVideoMapper;
import com.example.eduservice.service.EduChapterService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
    @Author:tjf
    @Description:
*/
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Resource
    private EduChapterMapper chapterMapper;
    @Resource
    private EduVideoMapper videoMapper;
    /**
     * 根据课程id，查询对应的章节
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVO> queryChapterByCourseId(String courseId) {
        List<ChapterVO> result = null;
        // 1 根据courseId，查询对应的章节
        LambdaQueryWrapper<EduChapter> chapterWrapper = new LambdaQueryWrapper<>();
        chapterWrapper.eq(EduChapter::getCourseId, courseId);
        List<EduChapter> chapterList = chapterMapper.selectList(chapterWrapper);
        if (chapterList.size() == 0){
            return result;
        }

        // 2 根据courseId，查询对应的小节
        LambdaQueryWrapper<EduVideo> videoWrapper = new LambdaQueryWrapper<>();
        videoWrapper.eq(EduVideo::getCourseId, courseId);
        List<EduVideo> videoList = videoMapper.selectList(videoWrapper);

        // 3 根据章节id，对小节进行分类封装
        result = this.sort(chapterList, videoList);
        return result;
    }

    /**
     * 根据id，删除章节，若章节有小节则不能删除
     * @param chapterId
     * @return true 删除成功 false 删除失败
     */
    @Override
    public boolean removeChapterById(String chapterId) {
        LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduVideo::getChapterId, chapterId);
        Long count = videoMapper.selectCount(wrapper);

        // 判断该章节下面是否有小节
        if (count > 0){
            throw new GuliException(20001, "请先删除该章节下的所有小节");
        }else {
            // 只有该章节没有任何小节且删除成功的情况下才返回true
            return chapterMapper.deleteById(chapterId) > 0;
        }
    }

    /**
     * 章节和小节的分类整理
     * @param chapterList
     * @param videoList
     * @return
     */
    private static List<ChapterVO> sort(List<EduChapter> chapterList, List<EduVideo> videoList) {
        List<ChapterVO> finalList = new ArrayList<>();
        List<VideoVO> videoVOList;

        // 遍历章节集合
        for (EduChapter chapter : chapterList) {
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(chapter, chapterVO);
            videoVOList = new ArrayList<>();

            // 遍历小节集合
            for (EduVideo video : videoList){
                // 若章节id和小节中的章节id相同则放到一起
                if (chapter.getId().equals(video.getChapterId())){
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(video, videoVO);
                    videoVOList.add(videoVO);
                }
            }
            finalList.add(chapterVO);
            // 设置章节的小节
            chapterVO.setChildren(videoVOList);
        }
        return finalList;
    }


}
